from bs4 import BeautifulSoup
import requests
import jieba
import wordcloud

# 豆瓣的请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
}

# 百度的请求头
baidu = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}

# 需要查询的电影名称
keyword = "长歌行"
# 百度的搜索的url
url = 'https://www.baidu.com/s?wd=' + keyword + '&ie=utf-8'
page = 5  # 每一页评论有20 条 输出5页


# 从百度搜索结果中获取豆瓣对应电影的url
def get_movie_url(douban_url):
    t = requests.get(url=douban_url, headers=headers)  # 请求网址
    soup = BeautifulSoup(str(t.text), 'html.parser')  # 文档对象
    get_movie_comment(t.request.url)
#   get_score(t.request.url)

# 获取此电影的评论


def get_movie_comment(movie_url):
    print(movie_url)
    for i in range(0, page):
        start = str(i * 20)
        temp_url = movie_url + 'comments?start=' + \
            start + '&limit=20&status=P&sort=new_score'
        t = requests.get(url=temp_url, headers=headers)  # 请求网址
        soup = BeautifulSoup(str(t.text), 'html.parser')  # 文档对象
        lt = soup.find_all('span', class_='short')
        for item in lt:
            print(item.string)
            open("./影评.txt", "a", encoding="utf-8").write(item.string)
            open("./影评.txt", "a", encoding="utf-8").write('\n')


def get_score(movie_url):
    for i in range(0, page):
        start = str(i * 20)
        temp_url = movie_url + 'comments?start=' + \
            start + '&limit=20&status=P&sort=new_score'
        t = requests.get(url=temp_url, headers=headers)  # 请求网址
        soup = BeautifulSoup(str(t.text), 'html.parser')  # 文档对象
        lt = soup.find_all('span', class_='allstar20 rating')
        for item in lt:
            print(item.string)
            open("F:\\星级.txt", "a").write(item.string)
            open("F:\\星级.txt", "a").write('\n')


r = requests.get(url=url, headers=baidu)  # 请求网址
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, 'html.parser')  # 文档对象
# print(r.text)
lis = soup.find_all('a')
for item in lis:
    if item.string == "豆瓣电影":
        douban_url = item.get('href')
        # print(douban_url)
        get_movie_url(douban_url)
# print(soup.find('div', class_='timeout-title').string)

f = open('./影评.txt', 'rb')
content1 = f.read()
f.close()
ls1 = jieba.lcut(content1)
txt1 = ' '.join(ls1)

#fnew = open('F:\\星级.txt', 'r', encoding='utf-8')
#content2 = fnew.read()
# fnew.close()
#ls2 = jieba.lcut(content2)
#txt2 = ' '.join(ls2)
stopword = ['的','了','啊']
w = wordcloud.WordCloud(font_path='./simfang.ttf', stopwords=stopword,
                        width=1000, height=700, background_color='white')
w.generate(txt1)
w.to_file('./'+keyword+'.png')
